// 引入接口文件
import {
  switchFun,
  findProduct
} from './areaListServ'
import { message } from 'antd'
import {cloneDeep} from 'lodash'

const defultState = {
    biz:{
      queryForm: {}
    },
    projectList: []
}

const tmpModule = {
  state: cloneDeep(defultState),
  subscriptions: {
    setup ({dispatch, history}) {
      history.listen(({ pathname, query }) => {
        if('' + tmpModule.pathname === '' + pathname){
          dispatch({ type: 'updateStore', payload: { ...cloneDeep(defultState), ...query } })//每次进去页面先重置状态
          dispatch({type: 'findProduct',payload: {}})
        }
      })
    },
  },


  effects: {
    *findProduct({payload},{put,select,call}){
      let {projectList} = yield select(state => state[tmpModule.namespace])
      let params = {
        type: 2,
      }
      const { success, msg, result } = yield call(findProduct, {...params})
      if(success){
        let list = result
        if(!list.length){
          return
        }
        projectList =  list.map((item,index)=>{
          return {
            label: item.areaName,
            value: item.areaId,
          }
        })
        yield put({type: 'updateStore', payload: {projectList}})
      }
    },
    *switchFun({payload},{put,select,call}){
      let params = {
        id: payload.id,
        tableType: 2,
        isEnable:payload.isEnable
      }
      const { success, msg, data } = yield call(switchFun, {...params})
      let defultState = yield select(state => state[tmpModule.namespace])
      if(success){
        message.success('提交成功')
        defultState.biz.refreshListDatas(true)
      }
    },
  },
  // 存入状态机
  reducers: {
    updateStore(preState, action) {
      return Object.assign({}, preState, action.payload)
    },
  }
}

export default tmpModule
